package com.example.mygame.controller;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mygame.R;
import com.example.mygame.pojo.User;
import com.example.mygame.service.SPSave;
import com.example.mygame.service.SqliteUtils;

import java.util.List;
import java.util.Map;

public class Login extends AppCompatActivity {

    private Button register;
    private Button login;
    private EditText code;
    private EditText password;
    private Button speedLogin;

    private SqliteUtils sqliteUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        register = findViewById(R.id.register);
        login = findViewById(R.id.login);
        speedLogin = findViewById(R.id.speed_login);

        code = findViewById(R.id.code);
        password = findViewById(R.id.password);

        sqliteUtils = new SqliteUtils(this);

        Intent getDataIntent = getIntent();

        Map<String, String> userInfo = SPSave.getUserInfo(this);

        String account = userInfo.get("account");

        String pwd = userInfo.get("password");

        String account_speed = getDataIntent.getStringExtra("userName");

        String password_speed = getDataIntent.getStringExtra("password");


        if (account_speed != null && password_speed != null) {
            code.setText(account_speed);
            password.setText(password_speed);
        } else {
            code.setText(account);
            password.setText(pwd);
        }


        List<User> userList = sqliteUtils.queryAllFromDb();



        // 注册方法
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login.this, Register.class);
                startActivity(intent);
            }
        });

        // 登录方法
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                List<User> userList1 = sqliteUtils.queryFromDbByName(code.getText().toString());

                for (User user : userList1) {
                    if (user.getPassword().equals(Login.this.password.getText().toString())) {
                        Toast.makeText(getApplicationContext(), "登录成功",
                                Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent(Login.this, Home.class);

                        Bundle bundle = new Bundle();

                        bundle.putString("userName",user.getName());
                        bundle.putString("password",user.getPassword());

                        intent.putExtras(bundle);

                        startActivity(intent);
                        return;
                    }
                }
                Toast.makeText(getApplicationContext(), "账号或密码错误",
                        Toast.LENGTH_SHORT).show();

            }
        });

        speedLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login.this, SpeedLogin.class);
                startActivity(intent);
            }
        });

    }


}